package com.king.service;

import com.king.mapper.UserMapper;
import com.king.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * com.king.service
 *
 * @Description: 服务类
 * @Author: kuaipeng
 * @Created: on 2019/12/06
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    /**新增保存用户*/
    @Transactional(rollbackFor = {})
    public void addUser(User user){
        //选择性新增；如果属性为空则该属性不会出现在insert语句上
        userMapper.insertSelective(user);
        System.out.println("新增用户成功!");
    }

    /**根据id查询*/
    public User queryById(Long id){
        return userMapper.selectByPrimaryKey(id);
    }

    /**根据ID修改用户*/
    @Transactional(rollbackFor = {})
    public void updateUser(User user){
        userMapper.updateByPrimaryKeySelective(user);
        System.out.println("修改用户成功!");
    }

    /**根据ID删除用户*/
    @Transactional(rollbackFor = {})
    public void deleteUser(Long id){
        userMapper.deleteByPrimaryKey(id);
        System.out.println("删除用户成功!");
    }

}
